// pages/my/cash/cash.js

const App = getApp()

Page({
  data: {
    cash: {
      items: [
        {
          name: '淘淘领返款',
          time: '2017-10-31 22:45:10',
          cash: '+20'
        },
        {
          name: '转帐',
          time: '2017-10-31 22:45:10',
          cash: '-30'
        },
        {
          name: '充值',
          time: '2017-10-31 22:45:10',
          cash: '+60'
        }        
      ],
      params: {
        page: 1,
        limit: 10,
      },
      paginate: { total: 3 }
    },

    prompt: {
      hidden: !0,
      icon: '../../../image/empty.png',
      title: '您还没有零钱！',
      text: '赶快去充值吧',
    },
  },

  onLoad() {
    //this.cash = App.HttpResource('/cash/:id', {id: '@id'})
    //this.onPullDownRefresh()
  },


  toRecharge(e) {
    console.log(e)
    App.WxService.navigateTo('/pages/my/cash/recharge')
  },


  initData() {
    this.setData({
      cash: {
        items: [],
        params: {
          page: 1,
          limit: 10,
        },
        paginate: {}
      }
    })
  },



  getList() {
    const cash = this.data.cash
    const params = cash.params

    // // App.HttpService.getCashList(params)
    this.cash.queryAsync(params)
      .then(res => {
        const data = res.data
        console.log(data)
        if (data.meta.code == 0) {
          cash.items = [...cash.items, ...data.data.items]
          cash.paginate = data.data.paginate
          cash.params.page = data.data.paginate.next
          cash.params.limit = data.data.paginate.perPage
          this.setData({
            cash: cash,
            'prompt.hidden': cash.items.length,
          })
        }
      })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.info('onPullDownRefresh')
    this.initData()
    this.getList()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.info('onReachBottom')
    if (!this.data.cash.paginate.hasNext) return
    this.getList()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }








})


